using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text.RegularExpressions;

namespace Patchy
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class PatchyForm : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Button SaveButton;
        private System.Windows.Forms.Label DateLabel;
        private System.Windows.Forms.Label ProjectNameLabel;
        private System.Windows.Forms.ComboBox ProjectNameComboBox;
        private System.Windows.Forms.Label BuildNumLabel;
        private System.Windows.Forms.TextBox BuildNumTextBox;
        private System.Windows.Forms.Label PatchNumberLabel;
        private System.Windows.Forms.TextBox PatchNumberTextBox;
        private System.Windows.Forms.Button FileBrowseButton;
        private System.Windows.Forms.TextBox FilePathTextBox;
        private System.Windows.Forms.GroupBox TypeGroupBox;
        private System.Windows.Forms.CheckBox BatFileCheckBox;
        private System.Windows.Forms.CheckBox MsiCheckBox;
        private System.Windows.Forms.GroupBox PatchDetailsGroupBox;
        private System.Windows.Forms.GroupBox PatchingFileGroupBox;
        private System.Windows.Forms.DateTimePicker DateTimePicker;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public PatchyForm(string[] args)
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

#if DEBUG
            if(args.Length == 0)
            {
                string[] init = { @"C:\Development\Patcher\Patchy2\bin\Debug\Product 1.14.008 - 1 - 09 October 2007.patchy" };
                args = init;
            }
#endif // DEBUG

            if (args.Length > 0 && File.Exists(args[0].ToString()))
            {
                this.FilePathTextBox.Text = args[0].ToString();
                Directory.SetCurrentDirectory(Path.GetDirectoryName(args[0].ToString()));
                if (args.Length > 1)
                {
                    MessageBox.Show("Arguments ignored.");
                }
                parseFileName(Path.GetFileName(args[0].ToString()));
                StateTest();
            }
        }

        //Product 1.14.008 -  - 09 October 2007
        private void parseFileName(string name)
        {
            string[] sect = name.Split('-', '_');
            switch (sect.Length)
            {
                case 1:
                    parseProject(sect[0].Trim());
                    break;
                case 2:
                    parseProject(sect[0].Trim());
                    parsePatchNumber(sect[1].Trim());
                    break;
                default:
                    parseProject(sect[0].Trim());
                    parsePatchNumber(sect[1].Trim());
                    parseDate(sect[2].Trim());
                    break;
            }
        }

        //Product 1.14.008
        private void parseProject(string project)
        {
            string buildNum = Regex.Match(project, @"[0-9]*$").ToString();
            if (buildNum != "")
            {
                project = project.Remove(project.Length - buildNum.Length, buildNum.Length).TrimEnd('.');
                if (!ProjectNameComboBox.Items.Contains(project))
                {
                    ProjectNameComboBox.Items.Add(project);
                }
                ProjectNameComboBox.Text = project;
                BuildNumTextBox.Text = buildNum;
            }
        }

        //Patch 16
        private void parsePatchNumber(string number)
        {
            string patchNum = Regex.Match(number, @"[0-9][A-zA-z0-9]*").ToString();
            if (patchNum != "")
            {
                PatchNumberTextBox.Text = patchNum;
            }
        }

        //09 October 2007
        private void parseDate(string date)
        {
            try
            {
                date = Regex.Replace(date, @"[\.A-Za-z]+$", "");
                DateTimePicker.Value = DateTime.Parse(date);
            }
            catch (Exception)
            {
            }
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SaveButton = new System.Windows.Forms.Button();
            this.DateLabel = new System.Windows.Forms.Label();
            this.DateTimePicker = new System.Windows.Forms.DateTimePicker();
            this.ProjectNameLabel = new System.Windows.Forms.Label();
            this.ProjectNameComboBox = new System.Windows.Forms.ComboBox();
            this.BuildNumLabel = new System.Windows.Forms.Label();
            this.BuildNumTextBox = new System.Windows.Forms.TextBox();
            this.PatchNumberLabel = new System.Windows.Forms.Label();
            this.PatchNumberTextBox = new System.Windows.Forms.TextBox();
            this.FileBrowseButton = new System.Windows.Forms.Button();
            this.FilePathTextBox = new System.Windows.Forms.TextBox();
            this.TypeGroupBox = new System.Windows.Forms.GroupBox();
            this.BatFileCheckBox = new System.Windows.Forms.CheckBox();
            this.MsiCheckBox = new System.Windows.Forms.CheckBox();
            this.PatchDetailsGroupBox = new System.Windows.Forms.GroupBox();
            this.PatchingFileGroupBox = new System.Windows.Forms.GroupBox();
            this.TypeGroupBox.SuspendLayout();
            this.PatchDetailsGroupBox.SuspendLayout();
            this.PatchingFileGroupBox.SuspendLayout();
            this.SuspendLayout();
            // 
            // SaveButton
            // 
            this.SaveButton.Enabled = false;
            this.SaveButton.Location = new System.Drawing.Point(104, 416);
            this.SaveButton.Name = "SaveButton";
            this.SaveButton.Size = new System.Drawing.Size(192, 24);
            this.SaveButton.TabIndex = 9;
            this.SaveButton.Text = "Generate and Save Patching Files";
            this.SaveButton.Click += new System.EventHandler(this.SaveButton_Click);
            // 
            // DateLabel
            // 
            this.DateLabel.Location = new System.Drawing.Point(16, 128);
            this.DateLabel.Name = "DateLabel";
            this.DateLabel.Size = new System.Drawing.Size(72, 16);
            this.DateLabel.TabIndex = 12;
            this.DateLabel.Text = "Date:";
            // 
            // DateTimePicker
            // 
            this.DateTimePicker.Location = new System.Drawing.Point(104, 128);
            this.DateTimePicker.Name = "DateTimePicker";
            this.DateTimePicker.Size = new System.Drawing.Size(120, 20);
            this.DateTimePicker.TabIndex = 11;
            // 
            // ProjectNameLabel
            // 
            this.ProjectNameLabel.Location = new System.Drawing.Point(16, 32);
            this.ProjectNameLabel.Name = "ProjectNameLabel";
            this.ProjectNameLabel.Size = new System.Drawing.Size(80, 16);
            this.ProjectNameLabel.TabIndex = 0;
            this.ProjectNameLabel.Text = "Project Name:";
            // 
            // ProjectNameComboBox
            // 
            this.ProjectNameComboBox.ItemHeight = 13;
            this.ProjectNameComboBox.Items.AddRange(new object[] {
            "Product1.14",
            "Product1.15",
            "Product1.16",
            "IIA1.0",
            "IIA2.0"});
            this.ProjectNameComboBox.Location = new System.Drawing.Point(104, 32);
            this.ProjectNameComboBox.Name = "ProjectNameComboBox";
            this.ProjectNameComboBox.Size = new System.Drawing.Size(208, 21);
            this.ProjectNameComboBox.TabIndex = 1;
            this.ProjectNameComboBox.Text = "Product1.14";
            this.ProjectNameComboBox.SelectedIndexChanged += new System.EventHandler(this.ProjectNameComboBox_SelectedIndexChanged);
            // 
            // BuildNumLabel
            // 
            this.BuildNumLabel.Location = new System.Drawing.Point(16, 64);
            this.BuildNumLabel.Name = "BuildNumLabel";
            this.BuildNumLabel.Size = new System.Drawing.Size(80, 16);
            this.BuildNumLabel.TabIndex = 2;
            this.BuildNumLabel.Text = "Build Number:";
            // 
            // BuildNumTextBox
            // 
            this.BuildNumTextBox.Location = new System.Drawing.Point(104, 64);
            this.BuildNumTextBox.Name = "BuildNumTextBox";
            this.BuildNumTextBox.Size = new System.Drawing.Size(56, 20);
            this.BuildNumTextBox.TabIndex = 3;
            this.BuildNumTextBox.Text = "00";
            this.BuildNumTextBox.TextChanged += new System.EventHandler(this.BuildNumTextBox_TextChanged);
            // 
            // PatchNumberLabel
            // 
            this.PatchNumberLabel.Location = new System.Drawing.Point(16, 96);
            this.PatchNumberLabel.Name = "PatchNumberLabel";
            this.PatchNumberLabel.Size = new System.Drawing.Size(80, 16);
            this.PatchNumberLabel.TabIndex = 4;
            this.PatchNumberLabel.Text = "Patch Number:";
            // 
            // PatchNumberTextBox
            // 
            this.PatchNumberTextBox.Location = new System.Drawing.Point(104, 96);
            this.PatchNumberTextBox.Name = "PatchNumberTextBox";
            this.PatchNumberTextBox.Size = new System.Drawing.Size(56, 20);
            this.PatchNumberTextBox.TabIndex = 5;
            this.PatchNumberTextBox.Text = "0";
            this.PatchNumberTextBox.TextChanged += new System.EventHandler(this.PatchNumberTextBox_TextChanged);
            // 
            // FileBrowseButton
            // 
            this.FileBrowseButton.Location = new System.Drawing.Point(16, 24);
            this.FileBrowseButton.Name = "FileBrowseButton";
            this.FileBrowseButton.Size = new System.Drawing.Size(144, 24);
            this.FileBrowseButton.TabIndex = 7;
            this.FileBrowseButton.Text = "Browse for patching file";
            this.FileBrowseButton.Click += new System.EventHandler(this.FileBrowseButton_Click);
            // 
            // FilePathTextBox
            // 
            this.FilePathTextBox.Location = new System.Drawing.Point(16, 64);
            this.FilePathTextBox.Name = "FilePathTextBox";
            this.FilePathTextBox.ReadOnly = true;
            this.FilePathTextBox.Size = new System.Drawing.Size(360, 20);
            this.FilePathTextBox.TabIndex = 8;
            // 
            // TypeGroupBox
            // 
            this.TypeGroupBox.Controls.Add(this.BatFileCheckBox);
            this.TypeGroupBox.Controls.Add(this.MsiCheckBox);
            this.TypeGroupBox.Location = new System.Drawing.Point(16, 320);
            this.TypeGroupBox.Name = "TypeGroupBox";
            this.TypeGroupBox.Size = new System.Drawing.Size(392, 80);
            this.TypeGroupBox.TabIndex = 10;
            this.TypeGroupBox.TabStop = false;
            this.TypeGroupBox.Text = "Patching Types";
            // 
            // BatFileCheckBox
            // 
            this.BatFileCheckBox.Checked = true;
            this.BatFileCheckBox.CheckState = System.Windows.Forms.CheckState.Checked;
            this.BatFileCheckBox.Location = new System.Drawing.Point(24, 48);
            this.BatFileCheckBox.Name = "BatFileCheckBox";
            this.BatFileCheckBox.Size = new System.Drawing.Size(152, 16);
            this.BatFileCheckBox.TabIndex = 1;
            this.BatFileCheckBox.Text = "Create Batch files";
            this.BatFileCheckBox.CheckedChanged += new System.EventHandler(this.BatFileCheckBox_CheckedChanged);
            // 
            // MsiCheckBox
            // 
            this.MsiCheckBox.Enabled = false;
            this.MsiCheckBox.Location = new System.Drawing.Point(24, 24);
            this.MsiCheckBox.Name = "MsiCheckBox";
            this.MsiCheckBox.Size = new System.Drawing.Size(160, 16);
            this.MsiCheckBox.TabIndex = 0;
            this.MsiCheckBox.Text = "Create MSI";
            this.MsiCheckBox.CheckedChanged += new System.EventHandler(this.MsiCheckBox_CheckedChanged);
            // 
            // PatchDetailsGroupBox
            // 
            this.PatchDetailsGroupBox.Controls.Add(this.BuildNumTextBox);
            this.PatchDetailsGroupBox.Controls.Add(this.BuildNumLabel);
            this.PatchDetailsGroupBox.Controls.Add(this.PatchNumberLabel);
            this.PatchDetailsGroupBox.Controls.Add(this.PatchNumberTextBox);
            this.PatchDetailsGroupBox.Controls.Add(this.DateLabel);
            this.PatchDetailsGroupBox.Controls.Add(this.DateTimePicker);
            this.PatchDetailsGroupBox.Controls.Add(this.ProjectNameLabel);
            this.PatchDetailsGroupBox.Controls.Add(this.ProjectNameComboBox);
            this.PatchDetailsGroupBox.Location = new System.Drawing.Point(16, 16);
            this.PatchDetailsGroupBox.Name = "PatchDetailsGroupBox";
            this.PatchDetailsGroupBox.Size = new System.Drawing.Size(392, 176);
            this.PatchDetailsGroupBox.TabIndex = 13;
            this.PatchDetailsGroupBox.TabStop = false;
            this.PatchDetailsGroupBox.Text = "Patch Details";
            // 
            // PatchingFileGroupBox
            // 
            this.PatchingFileGroupBox.Controls.Add(this.FileBrowseButton);
            this.PatchingFileGroupBox.Controls.Add(this.FilePathTextBox);
            this.PatchingFileGroupBox.Location = new System.Drawing.Point(16, 208);
            this.PatchingFileGroupBox.Name = "PatchingFileGroupBox";
            this.PatchingFileGroupBox.Size = new System.Drawing.Size(392, 104);
            this.PatchingFileGroupBox.TabIndex = 14;
            this.PatchingFileGroupBox.TabStop = false;
            this.PatchingFileGroupBox.Text = "Select Patching file";
            // 
            // PatchyForm
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(424, 454);
            this.Controls.Add(this.PatchingFileGroupBox);
            this.Controls.Add(this.PatchDetailsGroupBox);
            this.Controls.Add(this.SaveButton);
            this.Controls.Add(this.TypeGroupBox);
            this.MaximumSize = new System.Drawing.Size(432, 488);
            this.MinimumSize = new System.Drawing.Size(432, 488);
            this.Name = "PatchyForm";
            this.Text = "Patchy";
            this.TypeGroupBox.ResumeLayout(false);
            this.PatchDetailsGroupBox.ResumeLayout(false);
            this.PatchDetailsGroupBox.PerformLayout();
            this.PatchingFileGroupBox.ResumeLayout(false);
            this.PatchingFileGroupBox.PerformLayout();
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.Run(new PatchyForm(args));
        }

        private void StateTest()
        {
            SaveButton.Enabled = ProjectNameComboBox.Text != "" &&
                BuildNumTextBox.Text != "" &&
                PatchNumberTextBox.Text != "" &&
                FilePathTextBox.Text != "" &&
                (MsiCheckBox.Checked || BatFileCheckBox.Checked);
        }

        private void ProjectNameComboBox_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            StateTest();
        }

        private void BuildNumTextBox_TextChanged(object sender, System.EventArgs e)
        {
            StateTest();
        }

        private void PatchNumberTextBox_TextChanged(object sender, System.EventArgs e)
        {
            StateTest();
        }
        private void MsiCheckBox_CheckedChanged(object sender, System.EventArgs e)
        {
            StateTest();
        }

        private void BatFileCheckBox_CheckedChanged(object sender, System.EventArgs e)
        {
            StateTest();
        }

        private void FileBrowseButton_Click(object sender, System.EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = Directory.GetCurrentDirectory();
            Directory.SetCurrentDirectory(Directory.GetCurrentDirectory());//for later on.
            openFileDialog1.Filter = "patchy files (*.patchy)|*.patchy|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 1;
            //openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if (openFileDialog1.OpenFile() != null)
                    {
                        FilePathTextBox.Text = openFileDialog1.FileName;
                        parseFileName(Path.GetFileName(openFileDialog1.FileName));
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
            StateTest();
        }

        private void SaveButton_Click(object sender, System.EventArgs e)
        {
            PatchDetails details = new PatchDetails();
            details.PatchProjectName = ProjectNameComboBox.Text;
            details.PatchBuildNumber = BuildNumTextBox.Text;
            details.PatchNumber = PatchNumberTextBox.Text;
            details.PatchDateTime = DateTimePicker.Text;
            details.PatchFilePath = FilePathTextBox.Text;
            ArrayList types = new ArrayList();
           if (MsiCheckBox.Checked)
                types.Add(new CreateMsiFile());
            if (BatFileCheckBox.Checked)
                types.Add(new CreateBatchFiles());
            CreatePatchFiles pf = new CreatePatchFiles(details, types);
        }
    }
}
